class BaseTb {
	static get tb_name() {
		return ''
	}
	static get create_sql() {
		return ''
	}
	static page(params, page, cb) {
		if (!this.tb_name) return cb(new Error('请提供表名'))
		let sql = `SELECT * FROM ${this.tb_name} where 1=1 `
		if (params) {
			for (const key of Object.keys(params)) {
				if (params[key] != undefined) {
					sql += ` and ${key} = '${params[key]}' `
				}
			}
		}
		if (page && page.limit && page.offset) {
			sql += ` limit ${page.limit} offset ${page.offset} `
		}
		this.db.all(sql, cb)
	}
	static all(cb) {
		if (!this.tb_name) return cb(new Error('请提供表名'))
		this.db.all(`SELECT * FROM ${this.tb_name}`, cb)
	}
	static find(id, cb) {
		if (!this.tb_name) return cb(new Error('请提供表名'))
		this.db.get(`SELECT * FROM ${this.tb_name} WHERE id = ?`, id, cb)
	}
	static edit(data, where, cb) {
		if (!this.tb_name) return cb(new Error('请提供表名'))
		if (!data) return cb(new Error('请输入要修改的字段'))
		let sqlParatmp = []
		for (const key of Object.keys(data)) {
			sqlParatmp.push(` ${key} = '${data[key]}' `)
		}
		if (sqlParatmp.length <= 0) return cb(new Error('请输入要修改的字段'))
		let sqlWheretmp = []
		if (where) {
			for (const item of where) {
				sqlWheretmp.push(` ${item.sym ? item.sym : 'and'} ${item.key} = '${item.value}' `)
			}
		}
		const sql = `UPDATE ${this.tb_name} SET ${sqlParatmp.join(',')} WHERE 1=1 ${sqlWheretmp.join(' ')}`;
		this.db.run(sql, cb)
	}
	static create(data, cb) {
		if (!this.tb_name) return cb(new Error('请提供表名'))
		let sqlKeystmp = []
		let sqlValstmp = []
		for (const key of Object.keys(data)) {
			sqlKeystmp.push(key)
			sqlValstmp.push("'" + data[key] + "'")
		}
		const sql = `INSERT INTO ${this.tb_name}(${sqlKeystmp.join(',')}) VALUES (${sqlValstmp.join(',')})`;
		this.db.run(sql, cb)
	}
	static delete(id, cb) {
		if (!this.tb_name) return cb(new Error('请提供表名'))
		if (id) return cb(new Error('Please provide an id'))
		this.db.run(`DELETE FROM ${this.tb_name} WHERE id = '?'`, id, cb)
	}
}
module.exports = BaseTb
